/*     */ package com.newcrm.webapi;
/*     */ 
/*     */ import com.newcrm.bean.CompanyInfo;
/*     */ import com.newcrm.bean.ProductInfo;
/*     */ import com.newcrm.bean.User;
/*     */ import com.newcrm.config.Config;
/*     */ import com.newcrm.db.CompanyExtManager;
/*     */ import com.newcrm.db.CompanyManager;
/*     */ import com.newcrm.db.ConnectionManager;
/*     */ import com.newcrm.db.CustomerCategoryManager;
/*     */ import com.newcrm.db.CustomerManager;
/*     */ import com.newcrm.db.DealCategoryManager;
/*     */ import com.newcrm.db.GroupManager;
/*     */ import com.newcrm.db.NoteDefaultWordManager;
/*     */ import com.newcrm.db.NoteManager;
/*     */ import com.newcrm.db.TagManager;
/*     */ import com.newcrm.db.TaskCategoryManager;
/*     */ import com.newcrm.db.UserManager;
/*     */ import com.newcrm.db.UserVOIPManager;
/*     */ import com.newcrm.exception.ActiveCodeCheckException;
/*     */ import com.newcrm.exception.DBException;
/*     */ import com.newcrm.manager.ActiveCodeCheckManager;
/*     */ import com.newcrm.manager.CompanyStatusManager;
/*     */ import com.newcrm.util.MD5;
/*     */ import com.newcrm.util.Util;
/*     */ import java.io.IOException;
/*     */ import java.io.PrintWriter;
/*     */ import java.net.URLEncoder;
/*     */ import java.sql.SQLException;
/*     */ import java.util.HashMap;
/*     */ import java.util.Map;
/*     */ import javax.servlet.ServletContext;
/*     */ import javax.servlet.ServletException;
/*     */ import javax.servlet.http.Cookie;
/*     */ import javax.servlet.http.HttpServlet;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import javax.servlet.http.HttpServletResponse;
/*     */ import javax.servlet.http.HttpSession;
/*     */ import net.sf.json.JSONObject;
/*     */ import org.apache.commons.dbutils.DbUtils;
/*     */ import org.apache.log4j.Logger;
/*     */ import org.jsoup.Jsoup;
/*     */ import org.jsoup.nodes.Document;
/*     */ 
/*     */ public class Login extends HttpServlet
/*     */ {
/*     */   private static final long serialVersionUID = 6183365052866458749L;
/*  58 */   private static Logger logger = Logger.getLogger(Login.class.getName());
/*     */ 
/*     */   public void doGet(HttpServletRequest request, HttpServletResponse response)
/*     */     throws ServletException, IOException
/*     */   {
/*  83 */     response.setContentType("text/html;charset=utf-8");
/*  84 */     JSONObject jsonObject = doLogin(request, response);
/*  85 */     PrintWriter out = response.getWriter();
/*  86 */     out.println(jsonObject);
/*  87 */     out.flush();
/*  88 */     out.close();
/*     */   }
/*     */ 
/*     */   private JSONObject doLogin(HttpServletRequest request, HttpServletResponse response)
/*     */   {
/* 102 */     JSONObject jsonObject = new JSONObject();
/*     */ 
/* 104 */     String userId = Util.getURLParameter(request.getParameter("userId"));
/* 105 */     String password = 
/* 106 */       Util.getURLParameter(request.getParameter("password"));
/* 107 */     String captcha = Util.getURLParameter(request.getParameter("captcha"));
/* 108 */     String ip = Util.getClientIpAddr(request);
/*     */ 
/* 110 */     if ((userId == null) || (userId.trim().length() == 0) || (password == null) || 
/* 111 */       (password.trim().length() == 0) || (captcha == null) || 
/* 112 */       (captcha.trim().length() == 0)) {
/* 113 */       logger.warn("userId or password or captcha is null , userId: " + 
/* 114 */         userId + " / captcha: " + captcha);
/* 115 */       jsonObject.put("error", "1");
/* 116 */       return jsonObject;
/*     */     }
/*     */ 
/* 119 */     if (!Util.checkCode(request, captcha)) {
/* 120 */       logger.warn("登录IP：" + ip + "， 验证码输入错误。 userId: " + userId + 
/* 121 */         " / captcha: " + captcha);
/* 122 */       jsonObject.put("error", "2");
/* 123 */       return jsonObject;
/*     */     }
/*     */ 
/* 126 */     UserManager manager = new UserManager();
/* 127 */     String companyName = null;
/* 128 */     Integer companyId = null;
/*     */     try {
/* 130 */       Map map = manager.login(userId, password, false);
/* 131 */       companyId = (Integer)map.get("companyId");
/* 132 */       companyName = (String)map.get("companyName");
/*     */     } catch (Exception e) {
/* 134 */       logger.warn("login failed", e);
/*     */     }
/*     */ 
/* 137 */     if ((companyId == null) || (companyId.intValue() < 0)) {
/* 138 */       logger.warn("登录失败，登录IP：" + ip + "， 登录用户ID：" + userId);
/* 139 */       jsonObject.put("error", "1");
/* 140 */       return jsonObject;
/*     */     }
/*     */ 
/* 143 */     ProductInfo productInfo = Config.getInstance().productInfo;
/*     */ 
/* 145 */     if ("1".equals(productInfo.activeCodeFlag))
/*     */     {
/* 147 */       ActiveCodeCheckManager activeCodeManager = 
/* 148 */         ActiveCodeCheckManager.getInstance();
/*     */       try {
/* 150 */         activeCodeManager.checkActiveCode(companyId.intValue());
/*     */       } catch (ActiveCodeCheckException e) {
/* 152 */         logger.warn("登录失败，验证码检查失败：", e);
/* 153 */         jsonObject.put("error", "3");
/* 154 */         jsonObject.put("msg", e.getMessage());
/* 155 */         return jsonObject;
/*     */       }
/*     */     }
/*     */ 
/* 159 */     logger.warn("登录成功，IP：" + ip + "， 登录用户ID：" + userId);
/*     */ 
/* 161 */     StringBuffer sessionKey = new StringBuffer(userId);
/* 162 */     sessionKey.append(System.currentTimeMillis());
/* 163 */     String key = MD5.getMD5(sessionKey.toString());
/*     */     try
/*     */     {
/* 166 */       manager.updateSessionKey(userId, ip, key);
/*     */ 
/* 169 */       Cookie cookie = new Cookie("sessionKey", key);
/* 170 */       cookie.setMaxAge(31536000);
/* 171 */       response.addCookie(cookie);
/*     */ 
/* 174 */       String rememberMe = Util.getURLParameter(request
/* 175 */         .getParameter("rememberMe"));
/* 176 */       if ("1".equals(rememberMe)) {
/* 177 */         cookie = new Cookie("userId", userId);
/* 178 */         cookie.setMaxAge(31536000);
/* 179 */         response.addCookie(cookie);
/*     */       }
/*     */     } catch (Exception e) {
/* 182 */       logger.warn("update Session key failed", e);
/*     */     }
/*     */ 
/* 186 */     Map companys = (Map)getServletContext()
/* 187 */       .getAttribute("companys");
/*     */ 
/* 189 */     if (companys == null) {
/* 190 */       companys = new HashMap();
/* 191 */       getServletContext().setAttribute("companys", 
/* 192 */         companys);
/*     */     }
/*     */ 
/* 195 */     CompanyInfo company = (CompanyInfo)companys.get(companyId);
/* 196 */     if (company == null) {
/* 197 */       company = new CompanyInfo();
/* 198 */       company.id = companyId.intValue();
/* 199 */       company.name = companyName;
/* 200 */       companys.put(companyId, company);
/*     */ 
/* 203 */       CompanyManager companyManager = new CompanyManager();
/* 204 */       company.createDate = companyManager
/* 205 */         .getCompanyCreateDate(company.id);
/*     */     }CustomerCategoryManager customerCategoryManager;
/*     */     try {
/* 208 */       if (company.users == null)
/*     */       {
/* 210 */         company.users = manager.getCompanyUsers(companyId.intValue(), 
/* 211 */           null);
/*     */ 
/* 214 */         TaskCategoryManager taskCategoryManager = new TaskCategoryManager();
/* 215 */         DealCategoryManager dealCategoryManager = new DealCategoryManager();
/* 216 */         customerCategoryManager = new CustomerCategoryManager();
/* 217 */         NoteDefaultWordManager noteDefaultWordManager = new NoteDefaultWordManager();
/* 218 */         TagManager tagManager = new TagManager();
/* 219 */         GroupManager groupManager = new GroupManager();
/* 220 */         CompanyExtManager companyExtManager = new CompanyExtManager();
/*     */         try
/*     */         {
/* 223 */           company.taskCategorys = taskCategoryManager.list(companyId
/* 224 */             .intValue());
/* 225 */           company.dealCategorys = dealCategoryManager.list(companyId
/* 226 */             .intValue());
/* 227 */           company.customerCategorys = customerCategoryManager.list(
/* 228 */             companyId.intValue(), 1);
/* 229 */           company.setGroups(groupManager.list(companyId.intValue()));
/* 230 */           company.noteDefaultWords = noteDefaultWordManager
/* 231 */             .list(companyId.intValue());
/* 232 */           company.tags = tagManager.list(companyId.intValue());
/*     */ 
/* 235 */           company.closeGroupPublicDataFlag = companyExtManager
/* 236 */             .getFieldValue(
/* 237 */             companyId.intValue(), 
/* 238 */             12);
/*     */ 
/* 241 */           company.setPublicReadOnlyFlag = companyExtManager
/* 242 */             .getFieldValue(companyId.intValue(), 
/* 243 */             13);
/*     */ 
/* 262 */           String allowGroupList = companyExtManager.getFieldValue(
/* 263 */             companyId.intValue(), 
/* 264 */             11);
/*     */ 
/* 266 */           if ((allowGroupList == null) || 
/* 267 */             (allowGroupList.trim().length() == 0))
/*     */           {
/* 269 */             allowGroupList = "*";
/*     */           }
/* 271 */           company.allowPublicGroupList = allowGroupList;
/*     */         }
/*     */         catch (SQLException e) {
/* 274 */           logger.warn("get company task/deal/group category failed", 
/* 275 */             e);
/*     */         }
/*     */       }
/*     */     } catch (DBException e) {
/* 279 */       logger.warn("get company users failed", e);
/* 280 */       jsonObject.put("error", "1");
/* 281 */       return jsonObject;
/*     */     }
/* 283 */     User currentUser = null;
/* 284 */     for (User user : company.users) {
/* 285 */       if (userId.equals(user.userId)) {
/* 286 */         currentUser = user;
/* 287 */         break;
/*     */       }
/*     */     }
/* 290 */     if (currentUser == null) {
/* 291 */       jsonObject.put("error", "1");
/* 292 */       return jsonObject;
/*     */     }
/*     */ 
/*     */     try
/*     */     {
/* 297 */       long totalUserNum = 0L;
/* 298 */       long totalcustomerNum = 0L;
/* 299 */       long totalcustomerCategoryNum = 0L;
/* 300 */       long totalNoteNum = 0L;
/*     */ 
/* 302 */       java.sql.Connection conn = ConnectionManager.getInstance().getConnection();
/*     */       try
/*     */       {
/* 305 */         UserManager userManager = new UserManager();
/* 306 */         totalUserNum = userManager.getTotalNum(conn, companyId.intValue());
/*     */ 
/* 309 */         CustomerManager customerManager = new CustomerManager();
/* 310 */         totalcustomerNum = customerManager.getTotalNum(conn, companyId.intValue(), 
/* 311 */           0);
/*     */ 
/* 314 */         customerCategoryManager = new CustomerCategoryManager();
/* 315 */         totalcustomerCategoryNum = customerCategoryManager.getTotalNum(
/* 316 */           conn, companyId.intValue());
/*     */ 
/* 319 */         NoteManager noteManager = new NoteManager();
/* 320 */         totalNoteNum = noteManager.getTotalNum(conn, companyId.intValue());
/*     */       }
/*     */       finally {
/*     */         try {
/* 324 */           DbUtils.close(conn);
/*     */         } catch (SQLException e) {
/* 326 */           logger.warn("close conn failed", e);
/*     */         }
/*     */       }
/*     */ 
/* 330 */       String webUrl = "http://tj.qikebao.com/log?t=login&name=" + 
/* 331 */         URLEncoder.encode(companyName, "utf-8") + "&ver=" + 
/* 332 */         URLEncoder.encode("3.6.0.2", "utf-8") + "&u=" + 
/* 333 */         totalUserNum + "&c=" + totalcustomerNum + "&ca=" + 
/* 334 */         totalcustomerCategoryNum + "&n=" + totalNoteNum;
/*     */ 
/* 336 */       Document doc = 
/* 337 */         Jsoup.connect(webUrl)
/* 338 */         .userAgent(
/* 339 */         "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; Tablet PC 2.0)")
/* 340 */         .timeout(10000).get();
/* 341 */       if (doc != null) {
/* 342 */         String body = doc.text();
/* 343 */         CompanyStatusManager.getInstance().updateStatus(body);
/*     */       }
/*     */ 
/* 346 */       if (!CompanyStatusManager.getInstance().getLoginStatus()) {
/* 347 */         jsonObject.put("error", "1");
/* 348 */         return jsonObject;
/*     */       }
/*     */     } catch (Exception e) {
/* 351 */       logger.warn("report failed", e);
/*     */ 
///* 355 */       if ("admin".equals(userId)) {
///* 356 */         ReportLocalInfoManager.getInstance().clear();
///*     */       }
/*     */ 
/* 359 */       HttpSession session = request.getSession(true);
/* 360 */       session.setAttribute("user", currentUser);
/*     */ 
/* 363 */       UserVOIPManager userVOIPManager = new UserVOIPManager();
/*     */       try {
/* 365 */         Map voip = userVOIPManager
/* 366 */           .getUserVOIPSetting(currentUser.userId);
/*     */ 
/* 369 */         if (voip != null) {
/* 370 */           CompanyExtManager companyExtManager = new CompanyExtManager();
/*     */ 
/* 373 */           String serverIP = companyExtManager
/* 374 */             .getFieldValue(currentUser.companyId, 
/* 375 */             6);
/* 376 */           if (serverIP == null) {
/* 377 */             serverIP = "";
/*     */           }
/* 379 */           voip.put("ServerIP", serverIP);
/*     */ 
/* 382 */           String appId = companyExtManager.getFieldValue(
/* 383 */             currentUser.companyId, 
/* 384 */             4);
/* 385 */           if (appId == null) {
/* 386 */             appId = "";
/*     */           }
/* 388 */           voip.put("AppID", appId);
/*     */         }
/* 390 */         session.setAttribute("voip", voip);
/*     */       } catch (DBException de) {
/* 392 */         logger.warn("get voip info failed");
/*     */       } catch (SQLException se) {
/* 394 */         logger.warn("get voip info failed");
/*     */       }
/*     */ 
/* 397 */       jsonObject.put("error", "0");
/* 398 */     }return jsonObject;
/*     */   }
/*     */ 
/*     */   public void doPost(HttpServletRequest request, HttpServletResponse response)
/*     */     throws ServletException, IOException
/*     */   {
/* 418 */     doGet(request, response);
/*     */   }
/*     */ }

/* Location:           C:\E\referenceProject\CRM参考\qikebao\WEB-INF\lib\qikebao.jar
 * Qualified Name:     com.newcrm.webapi.Login
 * JD-Core Version:    0.6.1
 */